/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proiect.fis.UI;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.tree.DefaultTreeModel;
import proiect.fis.Generator;
import proiect.fis.UI.comboBoxModel.OrasModel;
import proiect.fis.UI.tabbedPane.TabbedPaneListener;
import proiect.fis.UI.tree.Tree;
import proiect.fis.UI.tree.Tree.TreeNode;
import proiect.fis.UI.tree.TreeNodeClickedListener;
import proiect.fis.commons.beans.Oras;
import proiect.fis.commons.beans.Pachet;
import proiect.fis.persistence.OrasDao;
import proiect.fis.persistence.PachetDao;

/**
 *
 * @author Boogie-Laptop
 */
public class MainForm extends javax.swing.JFrame implements TreeNodeClickedListener,
        TabbedPaneListener, NewPackageView.NewPackageListener, ChangeListener, NewCityView.NewCityViewListener {

    private Tree.TreeNode rootPachete;
    private Tree.TreeNode rootOrase;
    private List<Pachet> pachete;
    private List<Oras> orase;
    private PachetDao pDao;
    private OrasDao oDao;

    /**
     * Creates new form MainForm
     */
    public MainForm() {
        initComponents();

        saveCity.setVisible(false);
        editCity.setVisible(false);
        newCity.setVisible(false);

        pachete = new LinkedList<>();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Proiect_FIS_V2.0PU");
        pDao = new PachetDao(emf);
        oDao = new OrasDao(emf);
        OrasModel.oDao = oDao;
        rootPachete = new Tree.TreeNode("Pachete", null);
        rootOrase = new Tree.TreeNode("Orase", null);
        treePachete.setOpenIcon(new ImageIcon(MainForm.class.getResource("images/package_icon_open.png")));
        treePachete.setClosedIcon(new ImageIcon(MainForm.class.getResource("images/package_icon.png")));

        treeOrase.setOpenIcon(new ImageIcon(MainForm.class.getResource("images/city_icon.png")));
        treeOrase.setClosedIcon(new ImageIcon(MainForm.class.getResource("images/city_icon.png")));


        try {
            this.setIconImage(ImageIO.read(MainForm.class.getResource("images/despicablememinion.png")));
        } catch (IOException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setTitle("Proiect F.I.S Echipa 8");


        treePachete.addOnTreeNodeClickedListener(MainForm.this);
        treeOrase.addOnTreeNodeClickedListener(MainForm.this);

        tabbedPane.addTabbedPaneListener(MainForm.this);
        pachete = pDao.loadAll();
        for (Pachet pachet : pachete) {
            this.addPachet(pachet);
        }

        orase = oDao.loadAll();

        for (Oras o : orase) {
            this.addOras(o);
        }
        optionsTabPane.addChangeListener(this);

    }

    @Override
    public void stateChanged(ChangeEvent e) {
        JTabbedPane pane = (JTabbedPane) e.getSource();


        int sel = pane.getSelectedIndex();
        if (sel == 0) {
            //Pachete
            saveCity.setVisible(false);
            editCity.setVisible(false);
            newCity.setVisible(false);
            newPachet.setVisible(true);
            editPachet.setVisible(true);
            savePachet.setVisible(true);

        }
        if (sel == 1) {
            //orase
            saveCity.setVisible(true);
            editCity.setVisible(true);
            newCity.setVisible(true);
            newPachet.setVisible(false);
            editPachet.setVisible(false);
            savePachet.setVisible(false);

        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        before = new javax.swing.JPanel();
        optionsTabPane = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        treePachete = new proiect.fis.UI.tree.Tree();
        jScrollPane2 = new javax.swing.JScrollPane();
        treeOrase = new proiect.fis.UI.tree.Tree();
        north = new javax.swing.JPanel();
        savePachet = new javax.swing.JButton();
        editPachet = new javax.swing.JButton();
        newPachet = new javax.swing.JButton();
        editCity = new javax.swing.JButton();
        saveCity = new javax.swing.JButton();
        newCity = new javax.swing.JButton();
        center = new javax.swing.JPanel();
        tabbedPane = new proiect.fis.UI.tabbedPane.TabbedPane();
        startPage = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuItemNew = new javax.swing.JMenuItem();
        menuItemChitanta = new javax.swing.JMenuItem();
        menuItemExit = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(1024, 712));

        before.setPreferredSize(new java.awt.Dimension(200, 361));

        jScrollPane1.setViewportView(treePachete);

        optionsTabPane.addTab("Pachete", jScrollPane1);

        jScrollPane2.setViewportView(treeOrase);

        optionsTabPane.addTab("Orase", jScrollPane2);

        javax.swing.GroupLayout beforeLayout = new javax.swing.GroupLayout(before);
        before.setLayout(beforeLayout);
        beforeLayout.setHorizontalGroup(
            beforeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(optionsTabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
        );
        beforeLayout.setVerticalGroup(
            beforeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(beforeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(optionsTabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE))
        );

        getContentPane().add(before, java.awt.BorderLayout.LINE_START);

        north.setPreferredSize(new java.awt.Dimension(647, 80));

        savePachet.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_save_package.png"))); // NOI18N
        savePachet.setToolTipText("Save");
        savePachet.setBorderPainted(false);
        savePachet.setContentAreaFilled(false);
        savePachet.setFocusPainted(false);
        savePachet.setMargin(new java.awt.Insets(0, 0, 0, 0));
        savePachet.setMaximumSize(new java.awt.Dimension(47, 47));
        savePachet.setMinimumSize(new java.awt.Dimension(81, 81));
        savePachet.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_save_package_clicked.png"))); // NOI18N
        savePachet.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_save_package_rollover.png"))); // NOI18N
        savePachet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savePachetActionPerformed(evt);
            }
        });

        editPachet.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_edit_package.png"))); // NOI18N
        editPachet.setToolTipText("Edit");
        editPachet.setBorderPainted(false);
        editPachet.setContentAreaFilled(false);
        editPachet.setFocusPainted(false);
        editPachet.setMargin(new java.awt.Insets(0, 0, 0, 0));
        editPachet.setMaximumSize(new java.awt.Dimension(81, 81));
        editPachet.setMinimumSize(new java.awt.Dimension(81, 81));
        editPachet.setPreferredSize(new java.awt.Dimension(81, 81));
        editPachet.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_edit_package_clicked.png"))); // NOI18N
        editPachet.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_edit_package_rollover.png"))); // NOI18N
        editPachet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editPachetActionPerformed(evt);
            }
        });

        newPachet.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_new_package.png"))); // NOI18N
        newPachet.setToolTipText("Colet Nou");
        newPachet.setBorderPainted(false);
        newPachet.setContentAreaFilled(false);
        newPachet.setFocusPainted(false);
        newPachet.setMargin(new java.awt.Insets(0, 0, 0, 0));
        newPachet.setMaximumSize(new java.awt.Dimension(47, 47));
        newPachet.setMinimumSize(new java.awt.Dimension(81, 81));
        newPachet.setPreferredSize(new java.awt.Dimension(81, 81));
        newPachet.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_new_package_clicked.png"))); // NOI18N
        newPachet.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_new_package_rollover.png"))); // NOI18N
        newPachet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newPachetActionPerformed(evt);
            }
        });

        editCity.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_edit_city.png"))); // NOI18N
        editCity.setToolTipText("Save");
        editCity.setBorderPainted(false);
        editCity.setContentAreaFilled(false);
        editCity.setFocusPainted(false);
        editCity.setMargin(new java.awt.Insets(0, 0, 0, 0));
        editCity.setMaximumSize(new java.awt.Dimension(47, 47));
        editCity.setMinimumSize(new java.awt.Dimension(81, 81));
        editCity.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_edit_city_clicked.png"))); // NOI18N
        editCity.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_edit_city_rollover.png"))); // NOI18N
        editCity.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editCityActionPerformed(evt);
            }
        });

        saveCity.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_save_city.png"))); // NOI18N
        saveCity.setToolTipText("Save");
        saveCity.setBorderPainted(false);
        saveCity.setContentAreaFilled(false);
        saveCity.setFocusPainted(false);
        saveCity.setMargin(new java.awt.Insets(0, 0, 0, 0));
        saveCity.setMaximumSize(new java.awt.Dimension(47, 47));
        saveCity.setMinimumSize(new java.awt.Dimension(81, 81));
        saveCity.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_save_city_clicked.png"))); // NOI18N
        saveCity.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_save_city_rollover.png"))); // NOI18N
        saveCity.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveCityActionPerformed(evt);
            }
        });

        newCity.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_new_city.png"))); // NOI18N
        newCity.setToolTipText("Save");
        newCity.setBorderPainted(false);
        newCity.setContentAreaFilled(false);
        newCity.setFocusPainted(false);
        newCity.setMargin(new java.awt.Insets(0, 0, 0, 0));
        newCity.setMaximumSize(new java.awt.Dimension(47, 47));
        newCity.setMinimumSize(new java.awt.Dimension(81, 81));
        newCity.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_new_city_clicked.png"))); // NOI18N
        newCity.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/buttons/ic_menu_new_city_rollover.png"))); // NOI18N
        newCity.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newCityActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout northLayout = new javax.swing.GroupLayout(north);
        north.setLayout(northLayout);
        northLayout.setHorizontalGroup(
            northLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(northLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(newPachet, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(editPachet, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(savePachet, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(newCity, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editCity, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveCity, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(203, Short.MAX_VALUE))
        );
        northLayout.setVerticalGroup(
            northLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(northLayout.createSequentialGroup()
                .addGroup(northLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(northLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(editPachet, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addComponent(newPachet, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(savePachet, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveCity, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editCity, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(newCity, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(north, java.awt.BorderLayout.NORTH);

        startPage.setName("startPage");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/Proiect FIS Logo.PNG"))); // NOI18N
        startPage.add(jLabel1);

        tabbedPane.addTab("Pagina de start", startPage);

        javax.swing.GroupLayout centerLayout = new javax.swing.GroupLayout(center);
        center.setLayout(centerLayout);
        centerLayout.setHorizontalGroup(
            centerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        centerLayout.setVerticalGroup(
            centerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
        );

        getContentPane().add(center, java.awt.BorderLayout.CENTER);

        jMenu1.setText("File");

        menuItemNew.setText("New...");
        menuItemNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemNewActionPerformed(evt);
            }
        });
        jMenu1.add(menuItemNew);

        menuItemChitanta.setText("Printare Chitanta...");
        menuItemChitanta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemChitantaActionPerformed(evt);
            }
        });
        jMenu1.add(menuItemChitanta);

        menuItemExit.setText("Exit");
        menuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemExitActionPerformed(evt);
            }
        });
        jMenu1.add(menuItemExit);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void newPachetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newPachetActionPerformed
        newPachet();
    }//GEN-LAST:event_newPachetActionPerformed

    private void newPachet() {
        NewPackageView npv = new NewPackageView();
        npv.addNewPackageListner(this);
        npv.setVisible(true);
    }

    private void editPachetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editPachetActionPerformed
        Tree.TreeNode source = treePachete.getSelectedNode();
        if (source != null) {
            this.editPachet(source);
        }
    }//GEN-LAST:event_editPachetActionPerformed

    private void savePachetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savePachetActionPerformed
        pDao.updateAll(pachete);
        this.refreshTreePachete();
    }//GEN-LAST:event_savePachetActionPerformed

    private void menuItemChitantaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemChitantaActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setVisible(true);

        int res = jfc.showSaveDialog(this);
        if (res == JFileChooser.APPROVE_OPTION) {
            File f = jfc.getSelectedFile();
            try {
                BufferedWriter w = new BufferedWriter(new FileWriter(f));
                Generator g = new Generator();
                String[] s = g.generareChitanta((Pachet) treePachete.getSelectedNode().getValue());
                for (String string : s) {
                    w.write(string);
                    w.newLine();
                }

                w.close();

            } catch (IOException ex) {
                Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_menuItemChitantaActionPerformed

    private void menuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_menuItemExitActionPerformed

    private void menuItemNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemNewActionPerformed
        newPachet();
    }//GEN-LAST:event_menuItemNewActionPerformed

    private void editCityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editCityActionPerformed
        Tree.TreeNode source = treeOrase.getSelectedNode();
        if (source != null) {
            this.editOras(source);
        }
    }//GEN-LAST:event_editCityActionPerformed

    private void saveCityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveCityActionPerformed
        oDao.saveAll(orase);
    }//GEN-LAST:event_saveCityActionPerformed

    private void newCityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newCityActionPerformed
        NewCityView cv = new NewCityView();
        cv.addNewCityViewListner(this);
        cv.setVisible(true);

    }//GEN-LAST:event_newCityActionPerformed

    private void refreshTreePachete() {
        for (int i = 0; i < rootPachete.getChildCount(); i++) {
            Tree.TreeNode<Pachet> tp = (Tree.TreeNode<Pachet>) rootPachete.getChildAt(i);
            Pachet p = tp.getValue();
            tp.setName(p.getName());
        }
    }

    private void refreshTreeOrase() {
        for (int i = 0; i < rootOrase.getChildCount(); i++) {
            Tree.TreeNode<Oras> tp = (Tree.TreeNode<Oras>) rootOrase.getChildAt(i);
            Oras p = tp.getValue();
            tp.setName(p.getNume());
        }
    }

    private void editPachet(Tree.TreeNode source) {
        for (int i = 0; i < tabbedPane.getComponentCount(); i++) {
            if (tabbedPane.getComponent(i) instanceof PackageView) {
                PackageView tv = (PackageView) tabbedPane.getComponent(i);
                if (tv.getPachet().equals(source.getValue())) {
                    return;
                }
            }
        }
        Pachet p = (Pachet) source.getValue();
        PackageView pv = new PackageView();
        pv.setPachet(p);
        tabbedPane.addTab(p.getName(), pv);
        source.setIcon(new ImageIcon(MainForm.class.getResource("images/package_icon_leaf_open.png")));
        treePachete.repaint();
    }

    private void editOras(TreeNode source) {
        for (int i = 0; i < tabbedPane.getComponentCount(); i++) {
            if (tabbedPane.getComponent(i) instanceof CityView) {
                CityView tv = (CityView) tabbedPane.getComponent(i);
                if (tv.getOras().equals((Oras) source.getValue())) {
                    return;
                }
            }
        }

        Oras o = (Oras) source.getValue();
        CityView cv = new CityView();
        cv.setOras(o);
        System.out.println(o.getConexiune());
        tabbedPane.addTab(o.getNume(), cv);
        source.setIcon(new ImageIcon(MainForm.class.getResource("images/city_icon_leaf_open.png")));
        treeOrase.repaint();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /*
         * Create and display the form
         */


        new MainForm().setVisible(true);

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel before;
    private javax.swing.JPanel center;
    private javax.swing.JButton editCity;
    private javax.swing.JButton editPachet;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JMenuItem menuItemChitanta;
    private javax.swing.JMenuItem menuItemExit;
    private javax.swing.JMenuItem menuItemNew;
    private javax.swing.JButton newCity;
    private javax.swing.JButton newPachet;
    private javax.swing.JPanel north;
    private javax.swing.JTabbedPane optionsTabPane;
    private javax.swing.JButton saveCity;
    private javax.swing.JButton savePachet;
    private javax.swing.JPanel startPage;
    private proiect.fis.UI.tabbedPane.TabbedPane tabbedPane;
    private proiect.fis.UI.tree.Tree treeOrase;
    private proiect.fis.UI.tree.Tree treePachete;
    // End of variables declaration//GEN-END:variables

    @Override
    public void onTreeNodeClicked(MouseEvent me, Tree.TreeNode source) {
        if (me.getClickCount() == 2) {
            if (source.getValue() instanceof Pachet) {

                this.editPachet(source);

            } else if (source.getValue() instanceof Oras) {
                this.editOras(source);
            }
        }
    }

    @Override
    public void tabOpened(Component content, int index) {
        tabbedPane.setSelectedIndex(index);
    }

    @Override
    public void tabClosed(Component c, int index) {
        if (c instanceof PackageView) {

            PackageView tv = (PackageView) c;
            if (!tv.isValidInput()) {
                JOptionPane.showMessageDialog(this, "Campurile nu au fost completate corect! Modificarile nu au fost salvate", "Eroare", JOptionPane.ERROR_MESSAGE);
                return;
            }
            Pachet p = tv.getPachet();
            //System.out.println(p);

            treePachete.findTreeNode(tv.getPachet()).setIcon(new ImageIcon(MainForm.class.getResource("images/package_icon_leaf.png")));
            treePachete.repaint();

            pDao.update(p);

            oDao.updateAll(p.getRuta());
            this.refreshTreePachete();
        }
        if (c instanceof CityView) {
            CityView tv = (CityView) c;
            Oras p = tv.getOras();
            //System.out.println(p);

            treeOrase.findTreeNode(tv.getOras()).setIcon(new ImageIcon(MainForm.class.getResource("images/city_icon_leaf.png")));
            treeOrase.repaint();

            oDao.update(p);

            this.refreshTreeOrase();
        }
    }

    private void addPachet(Pachet p) {

        Tree.TreeNode t = new Tree.TreeNode(p.getName(), p);
        t.setIcon(new ImageIcon(MainForm.class.getResource("images/package_icon_leaf.png")));
        rootPachete.add(t);

        treePachete.setModel(
                new DefaultTreeModel(rootPachete));
        treePachete.repaint();
    }

    private void addOras(Oras o) {
        Tree.TreeNode<Oras> t = new Tree.TreeNode<>(o.getNume(), o);
        t.setIcon(new ImageIcon(MainForm.class.getResource("images/city_icon_leaf.png")));
        rootOrase.add(t);

        treeOrase.setModel(
                new DefaultTreeModel(rootOrase));
        treeOrase.repaint();
    }

    @Override
    public void onNewPackageCreated(Pachet p) {
        pDao.save(p);
        addPachet(p);
        pachete.add(p);

    }

    @Override
    public void onNewCityCreated(Oras o) {
        oDao.save(o);
        addOras(o);
        orase.add(o);
    }
}
